/**
 *  @file SDLRectangleRepresentation.cpp
 *
 *  @date 26-nov-2009
 *  @author Ives van der Flaas
 */

#include <SDL_gfxPrimitives.h>
#include "SDLRectangleRepresentation.h"
#include "SDLSurfaceDeleter.h"
#include "SDLZoomDrawer.h"
#include "SDLVideoSurfaceDeleter.h"
#include "SDLColor.h"

namespace si_sdl
{

SDLRectangleRepresentation::SDLRectangleRepresentation(
		si::Vector2 pos,
		si::Vector2 size,
		Color color):
			fPos(pos),
			fSize(size),
			fColor(color)
{

	fMe = std::tr1::shared_ptr<SDL_Surface>(
			SDL_CreateRGBSurface(
					SDL_SWSURFACE,
					size.getX(), size.getY(),
					SDL_GetVideoSurface()->format->BitsPerPixel,
					0,0,0,0),
			SDLSurfaceDeleter());

	fResized = fMe;

	drawOnSurface(si::Vector2(0,0),size, color, fMe);



}

void SDLRectangleRepresentation::drawRect(
				si::Vector2 absPosition,
				si::Vector2 absSize,
				Color color)
{
	drawOnSurface(
			absPosition,
			absSize,
			color,
			std::tr1::shared_ptr<SDL_Surface>(SDL_GetVideoSurface(), SDLVideoSurfaceDeleter()));
}
void SDLRectangleRepresentation::drawOnSurface(
				si::Vector2 pos,
				si::Vector2 virtualSize,
				Color color,
				std::tr1::shared_ptr<SDL_Surface> surfToDrawOn)
{

	si::Vector2 realSize;
	si::Vector2 offset;

	SDLZoomDrawer::get()->requestRealSizeAndOffset(virtualSize, realSize, offset);


	pos += offset;

	SDL_Rect r;
	r.x = pos.getX();
	r.y = pos.getY();
	r.w = virtualSize.getX();
	r.h = virtualSize.getY();

	SDL_FillRect(
			surfToDrawOn.get(),
			&r,
			SDL_MapRGB(
					SDL_GetVideoSurface()->format,
					color.getR(),
					color.getG(),
					color.getB()));


}


SDLRectangleRepresentation::~SDLRectangleRepresentation()
{
	// TODO Auto-generated destructor stub
}


}
